package net.lab1024.sa.admin.module.users.borrow.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.lab1024.sa.admin.module.users.borrow.dao.EquipmentBorrowApplicationDao;
import net.lab1024.sa.admin.module.users.borrow.domain.entity.EquipmentBorrowApplicationEntity;
import net.lab1024.sa.admin.module.users.borrow.domain.form.*;
import net.lab1024.sa.admin.module.users.borrow.domain.vo.EquipmentBorrowApplicationVO;
import net.lab1024.sa.admin.module.users.equipmentmanage.dao.EquipmentManageDao;
import net.lab1024.sa.admin.module.users.equipmentmanage.domain.entity.EqStatus;
import net.lab1024.sa.admin.module.users.equipmentmanage.domain.entity.EquipmentManageEntity;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实训室耗材申请 Service
 *
 * @Author 王俊权
 * @Date 2024-11-08 13:31:04
 * @Copyright 🐂🐎bloc
 */

@Service
public class EquipmentBorrowApplicationService {

    @Resource
    private EquipmentBorrowApplicationDao equipmentBorrowApplicationDao;

    @Resource
    private EquipmentManageDao equipmentManageDao;

    private final static String ADMIN = "管理员";
    private final static String TEACHER = "指导老师";
    // 同意
    private final static Integer AGREE = 1;
    // 不同意
    private final static Integer DISAGREE = 2;
    //已经归还
    private final static Integer RETURNED = 1;



    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<EquipmentBorrowApplicationVO> queryPage(EquipmentBorrowApplicationQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<EquipmentBorrowApplicationVO> list = equipmentBorrowApplicationDao.queryPage(page, queryForm);
        PageResult<EquipmentBorrowApplicationVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */


    public ResponseDTO<String> add(EquipmentBorrowApplicationAddForm addForm) {
        EquipmentBorrowApplicationEntity equipmentBorrowApplicationEntity = SmartBeanUtil.copy(addForm, EquipmentBorrowApplicationEntity.class);
        int insert = equipmentBorrowApplicationDao.insert(equipmentBorrowApplicationEntity);
        if (insert >0 && addForm.getEquipmentId()!= null){
            EquipmentManageEntity equipmentManageEntity = new EquipmentManageEntity();
            equipmentManageEntity.setId(addForm.getEquipmentId());
            equipmentManageEntity.setStatus(1);
            equipmentManageDao.updateById(equipmentManageEntity);
        }
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(EquipmentBorrowApplicationUpdateForm updateForm) {
        EquipmentBorrowApplicationEntity equipmentBorrowApplicationEntity = SmartBeanUtil.copy(updateForm, EquipmentBorrowApplicationEntity.class);
        equipmentBorrowApplicationDao.updateById(equipmentBorrowApplicationEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        equipmentBorrowApplicationDao.deleteBatchIds(idList);

        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }
        EquipmentBorrowApplicationEntity equipmentBorrowApplicationEntity = equipmentBorrowApplicationDao.selectById(id);
        Integer equipmentId = equipmentBorrowApplicationEntity.getEquipmentId();
        int i = equipmentBorrowApplicationDao.deleteById(id);
        if (i > 0 && equipmentId!= null){
//            System.out.println("i"+i);
            EquipmentManageEntity equipmentManageEntity = new EquipmentManageEntity();
            equipmentManageEntity.setId(equipmentId);
            equipmentManageEntity.setStatus(0);
            equipmentManageDao.updateById(equipmentManageEntity);
        }


        return ResponseDTO.ok();
    }

    /**
      *@author at_bear
      *@description happy设备审批
      */


    public ResponseDTO<String> approve(EquipmentBorrowApplicationApprovalForm approvalFrom){
        EquipmentBorrowApplicationEntity eqBorrowEntity = SmartBeanUtil.copy(approvalFrom, EquipmentBorrowApplicationEntity.class);
        Integer adminSignature = eqBorrowEntity.getAdminSignature();
        Integer teacherSignature = eqBorrowEntity.getGuideTeacherSignature();
        EquipmentBorrowApplicationEntity eqBorrowList = new EquipmentBorrowApplicationEntity();
        eqBorrowList.setId(eqBorrowEntity.getId());
        eqBorrowList.setAdminRemark(eqBorrowEntity.getAdminRemark());
        eqBorrowList.setAdminSignature(adminSignature);
        eqBorrowList.setGuideTeacherSignature(teacherSignature);
        eqBorrowList.setEquipmentId(eqBorrowEntity.getEquipmentId());
        eqBorrowList.setTeacherRemark(eqBorrowEntity.getTeacherRemark());
        if(adminSignature == AGREE && teacherSignature == AGREE){
            EquipmentManageEntity equipmentManageEntity = new EquipmentManageEntity();
            equipmentManageEntity.setId(eqBorrowEntity.getEquipmentId());
            equipmentManageEntity.setStatus(EqStatus.BORROWING);
            equipmentManageDao.updateById(equipmentManageEntity);
            //实际借用日期
            Date date = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Timestamp timestamp = Timestamp.valueOf(nowTime);
            eqBorrowList.setBorrowDate(timestamp);

        }else if(adminSignature == DISAGREE || teacherSignature == DISAGREE){
            EquipmentManageEntity equipmentManageEntity = new EquipmentManageEntity();
            equipmentManageEntity.setId(eqBorrowEntity.getEquipmentId());
            equipmentManageEntity.setStatus(EqStatus.AVAILABLE);
            equipmentManageDao.updateById(equipmentManageEntity);
        }
        equipmentBorrowApplicationDao.updateById(eqBorrowList);
        return ResponseDTO.ok();
    }
    /**
      *@author at_bear
      *@description happy设备归还审核
      */
    public ResponseDTO<String> returnEqAdmin(Integer id){
        EquipmentBorrowApplicationEntity eqf = equipmentBorrowApplicationDao.selectById(id);
        EquipmentBorrowApplicationEntity eqRrEntity = new EquipmentBorrowApplicationEntity();
        eqRrEntity.setId(id);
        //归还日期
        Date date = new Date();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        Timestamp timestamp = Timestamp.valueOf(nowTime);
        eqRrEntity.setReturnDate(timestamp);
        //归还字段
        eqRrEntity.setReturnConfirmation(RETURNED);

        // 归还后状态
        EquipmentManageEntity equipmentManageEntity = new EquipmentManageEntity();
        equipmentManageEntity.setId(eqf.getEquipmentId());
        equipmentManageEntity.setStatus(EqStatus.AVAILABLE);
        equipmentManageDao.updateById(equipmentManageEntity);

        equipmentBorrowApplicationDao.updateById(eqRrEntity);

        return ResponseDTO.ok();

    }


    /**
      *@author at_bear
      *@description happy归还设备
      */
    public ResponseDTO<String> returnEq(EquipmentReturnForm returnForm){
        EquipmentBorrowApplicationEntity eqRrEntity = SmartBeanUtil.copy(returnForm, EquipmentBorrowApplicationEntity.class);
        EquipmentBorrowApplicationEntity eqBorrowEntity = new EquipmentBorrowApplicationEntity();
        eqBorrowEntity.setId(eqRrEntity.getId());
        eqBorrowEntity.setReturnStatus(RETURNED);
        // 归还图片id
        eqBorrowEntity.setReturnFileId(eqRrEntity.getReturnFileId());

        equipmentBorrowApplicationDao.updateById(eqBorrowEntity);

        return ResponseDTO.ok();
    }

}
